package com.example.demo.service.impl;

import com.example.demo.dao.LoginMapper;
import com.example.demo.pojo.TokenData;
import com.example.demo.pojo.User;
import com.example.demo.service.LoginService;
import com.example.demo.utils.MD5Util;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 登录Service
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    /**
     * 登录业务逻辑
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {

        User u = this.loginMapper.findUserByStunum(user.getStunum());

        if( u == null){
            return ResultFactory.failureWithStatus(999,"该用户不存在");
        }else if(!u.getPassword().equals(MD5Util.stringToMD5(user.getPassword()))){
            return ResultFactory.failureWithStatus(999,"密码错误");
        }else if(u.getState() == 4){
            return  ResultFactory.failureWithStatus(999,"该用户已注销");
        }else{
            //1-正常,2暂停,3-挂失,4-注销
            TokenData tokenData=new TokenData(u.getReader_id(),u.getUser_state(),u.getState(),u.getName());
            Result result = ResultFactory.successWithStatusAndData(200,"登陆成功",tokenData);
            return result;
        }
    }
}
